package com.bifang.module.file.core.biz.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.util.TreeUtil;
import com.bifang.module.base.common.vo.tree.TreeRetVO;
import com.bifang.module.base.common.vo.tree.TreeVO;
import com.bifang.module.file.common.vo.fileCollect.FileCollectTreeVO;
import com.bifang.module.file.dao.model.FileClass;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.module.file.common.vo.fileCollect.FileCollectVO;
import com.bifang.module.file.common.vo.fileCollect.FileCollectKeyVO;
import com.bifang.module.file.common.vo.fileCollect.FileCollectQueryVO;
import com.bifang.module.file.core.biz.FileCollectIntegration;
import com.bifang.module.file.dao.service.FileCollectService;
import com.bifang.module.file.dao.model.FileCollect;
/**
 * 文件归集管理业务实现类
 *
 * @author shun
 * @since 2023-03-22
 */
@ApiService(funcCode = "fileCollect", title = "文件归集管理")
public class FileCollectIntegrationImpl implements FileCollectIntegration {

    @Autowired
    public FileCollectService fileCollectService;

    @Override
    @OpApi(funcCode = "fileCollect0001", title = "文件归集管理分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<FileCollectVO> getList(FileCollectQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<FileCollect> list = fileCollectService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<FileCollect> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<FileCollectVO> resultList = Convert.toList(FileCollectVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "fileCollect0002", title = "文件归集管理根据主键查询详情", funcType = FuncTypeEnum.query)
    public FileCollectVO getById(FileCollectKeyVO vo) {
        FileCollect entity = fileCollectService.getById(vo.getFileCollectCode());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        FileCollectVO result = Convert.convert(FileCollectVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "fileCollect0003", title = "文件归集管理保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(FileCollectVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "fileCollect0004", title = "文件归集管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "fileCollect0004",
            operationName = "文件归集管理新增",
            dataType = "fileCollectCode",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileCollectVO add(FileCollectVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        FileCollect entity = Convert.convert(FileCollect.class, vo);
        fileCollectService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setFileCollectCode(entity.getFileCollectCode());
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileCollect0005", title = "文件归集管理修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "fileCollect0005",
            operationName = "文件归集管理修改",
            dataType = "fileCollectCode",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileCollectVO edit(FileCollectVO vo) {
        if (ObjectUtils.isEmpty(vo.getFileCollectCode())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        FileCollect oldEntity = fileCollectService.getById(vo.getFileCollectCode());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        FileCollect entity = Convert.convert(FileCollect.class, vo);
        fileCollectService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileCollect0006", title = "文件归集管理根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "fileCollect0006",
            operationName = "文件归集管理删除",
            dataType = "fileCollectCode",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(FileCollectKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getFileCollectCode())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        FileCollect entity = fileCollectService.getById(vo.getFileCollectCode());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        fileCollectService.removeById(vo.getFileCollectCode());
    }

    @Override
    @OpApi(
            funcCode = "fileCollect0007",
            title = "获取归集分类树",
            funcType = FuncTypeEnum.query,
            publicFlag = BoolEnum.TRUE,
            savelog = false)
    public TreeRetVO getFileCollectTree(FileCollectTreeVO vo) {
        boolean lazy=false;
        if(vo.getLazy()!=null){
            lazy= vo.getLazy();
        }
        String searchText = vo.getSearchText();
        List<FileCollect> fileCollectList=new ArrayList<>();
        if(lazy){
            String parentKey = vo.getParentKey();
            if(StringUtils.isBlank(parentKey)){
                QueryWrapper<FileCollect> queryWrapper=new QueryWrapper<>();
                queryWrapper.lambda().eq(FileCollect::getParentCode,null);
                List<FileCollect> list = fileCollectService.list(queryWrapper);
                fileCollectList.addAll(list);
            }else{
                QueryWrapper<FileCollect> queryWrapper=new QueryWrapper<>();
                queryWrapper.lambda().eq(FileCollect::getParentCode,parentKey);
                List<FileCollect> list = fileCollectService.list(queryWrapper);
                fileCollectList.addAll(list);
            }
        }else{
            QueryWrapper<FileCollect> queryWrapper=new QueryWrapper<>();
            if(StringUtils.isNotBlank(searchText)){
                //判断是否为中文,中文按分类名称查询,其他按编码查询
                if(Validator.isChinese(searchText)){
                    queryWrapper.lambda().like(FileCollect::getCollectTitle,searchText);
                }else{
                    queryWrapper.lambda().like(FileCollect::getFileCollectCode,searchText);
                }
            }
            List<FileCollect> list = fileCollectService.list(queryWrapper);
            fileCollectList.addAll(list);
        }
        TreeRetVO treeRetVO = new TreeRetVO();
        if (CollectionUtils.isEmpty(fileCollectList)) {
            treeRetVO.setTreeList(new ArrayList<>());
            return treeRetVO;
        }
        List<TreeVO> antTreeVOS=new ArrayList<>();
        for(FileCollect fileCollect:fileCollectList){
            TreeVO antTreeVO=new TreeVO();
            antTreeVO.setKey(fileCollect.getFileCollectCode());
            antTreeVO.setParentKey(fileCollect.getParentCode());
            antTreeVO.setTitle(fileCollect.getCollectTitle());
            if(StringUtils.isNotBlank(searchText)){
                antTreeVO.setLoaded(true);
                antTreeVO.setExpanded(true);
            }else{
                antTreeVO.setLoaded(false);
                antTreeVO.setExpanded(false);
            }
            Map<String,Object> map =new HashMap<>();
            map.put("title","title");
            antTreeVO.setScopedSlots(map);
            antTreeVOS.add(antTreeVO);
        }
        if(lazy){
            treeRetVO.setTreeList(antTreeVOS);
            return treeRetVO;
        }
        treeRetVO.setTreeList(TreeUtil.constructTaskDTOToTree(antTreeVOS));
        return treeRetVO;
    }
}
